﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.DAO
{
    public class OrderB2B_DAO
    {
        private static QL_DoanhNghiepDataContext data = new QL_DoanhNghiepDataContext();

        public static void SaveDetailOrderB2B(ORDER_DETAIL detailorder)
        {
            data.spSaveDetailOrder(detailorder.ElectricDevice_ID, detailorder.OrderB2B_ID, detailorder.Quantity, detailorder.Cost);
        }

        public static void DeleteCart(string OrderB2B_ID)
        {
            data.spDeleteCart(OrderB2B_ID);
        }

        public static void GetOrderID(int UserID, ref string OrderB2B_ID)
        {
            var listtemp = from o in data.ORDER_B2Bs
                           where o.UserID == UserID && o.status == 0
                           select o;
            if (listtemp.Count() > 0)
            {
                OrderB2B_ID = listtemp.First().OrderB2B_ID;
            }
        }

        public static void SaveOrderB2B(ORDER_B2B order, ref string orderb2b_id)
        {
            if (orderb2b_id == "")
            {
                var list = from o in data.ORDER_B2Bs
                           orderby o.OrderedDate
                           select new
                               {
                                   o.OrderB2B_ID,
                               };
                string ID = list.ToList()[list.Count() - 1].OrderB2B_ID.Substring(7);
                order.OrderB2B_ID = "OB2B000" + (int.Parse(ID) + 1).ToString();
                orderb2b_id = order.OrderB2B_ID;
            }
            else
            {
                order.OrderB2B_ID = orderb2b_id;
            }
            data.spSaveOrderB2B(order.OrderB2B_ID, order.UserID, order.OrderedDate, order.TotalPrice);
        }

        public static Cart GetSaveDetailOrderB2B(string UserID)
        {
            var list = from od in data.ORDER_DETAILs
                       join o in data.ORDER_B2Bs on od.OrderB2B_ID equals o.OrderB2B_ID
                       where o.UserID == int.Parse(UserID) && o.status == 0
                       select new
                       {
                           o.OrderB2B_ID,
                           od.ElectricDevice_ID,
                           od.Quantity,
                       };
            Cart cart = new Cart();
            foreach (var item in list)
            {
                cart.OrderB2BID = item.OrderB2B_ID;
                MyOrderProduct_Online myorder = new MyOrderProduct_Online();
                myorder.ElectricDeviceID = item.ElectricDevice_ID;
                var listemp = ElectricDevice_DAO.DetailElectricDevice(myorder.ElectricDeviceID);
                if (listemp.Count > 0)
                {
                    myorder.RetailPrice = decimal.Parse(listemp[0].RetailPrice);
                    myorder.DealerPrice = decimal.Parse(listemp[0].DealerPrice);
                    myorder.Quantity = (int)item.Quantity;
                    string NonDiscountPrice = "";
                    string DiscountPrice = "";
                    Coupon_DAO.GetPriceCouponOfElectricDevice(myorder.ElectricDeviceID, ref NonDiscountPrice, ref DiscountPrice);

                    if (NonDiscountPrice != "")
                        myorder.PriceCoupon = decimal.Parse(NonDiscountPrice);
                    else
                        myorder.PriceCoupon = 0;
                    cart.AddToCart(myorder);
                }
                
            }
            return cart;
        }

        public static void DeleteDetailOrderB2B(ORDER_DETAIL order)
        {
            data.spDeleteDetailOrder(order.ElectricDevice_ID, order.OrderB2B_ID);
        }

        public static void GetOrderB2B_ID(int UserID, ref string OrderB2B_ID)
        {
            var list = from o in data.ORDER_B2Bs
                       where o.UserID == UserID && o.status == 0
                       select o;
            if (list.Count() > 0)
                OrderB2B_ID = list.First().OrderB2B_ID;

        }

        internal static bool checkExistOrderB2BID(string orderB2BID)
        {
            try
            {
                var result = from o in data.ORDER_B2Bs
                             where o.OrderB2B_ID.Equals(orderB2BID)
                             select o;
                if (result.Count() >= 1)
                {
                    //ton tai
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
